#pragma once

#include "dut/WinMin.h"
#include "dut/DXTrace.h"

#include <d3d11.h>
#include <wrl/client.h>
#include <iostream>
#include <string>

using namespace Microsoft::WRL;

namespace Dut
{
	struct UploadBuffer
	{

		ComPtr<ID3D11Buffer> m_pConstantBuffer;        

		void VSSet(ID3D11DeviceContext* context, unsigned int startSlot, unsigned int numSlot = 1);
		void PSSet(ID3D11DeviceContext* context, unsigned int startSlot, unsigned int numSlot = 1);


		template<typename T>
		HRESULT Create(ID3D11Device* device)
		{

			HRESULT hr;

			D3D11_BUFFER_DESC cbd;
			ZeroMemory(&cbd, sizeof(cbd));
			cbd.Usage = D3D11_USAGE_DYNAMIC;
			cbd.ByteWidth = sizeof(T);
			cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
			cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;

			HR(device->CreateBuffer(&cbd, nullptr, m_pConstantBuffer.GetAddressOf()));

			return S_OK;
		}

		template<typename T>
		void Upload(ID3D11DeviceContext* context, const T& t)
		{

			D3D11_MAPPED_SUBRESOURCE mappedData;
			HR(context->Map(m_pConstantBuffer.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData));
			memcpy_s(mappedData.pData, sizeof(T), &t, sizeof(T));
			context->Unmap(m_pConstantBuffer.Get(), 0);
		}

	};
};